package advance.app.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件压缩工具类
 */
public class ZipUtil {
    private static final int BUFFER_SIZE = 2 * 1024;
	/**
	 * 是否保留原来的目录结构
	 * true:  保留目录结构;
	 * false: 所有文件跑到压缩包根目录下(注意：不保留目录结构可能会出现同名文件,会压缩失败)
	 */
	private static final boolean KeepDirStructure = true;
	private static final Logger log = LoggerFactory.getLogger(ZipUtil.class);
	
	/*public static void main(String[] args) {
		try {
			toZip("E:/app1", "E:/app.zip",true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	/**
	 * 压缩成ZIP
	 * @param srcDir         压缩 文件/文件夹 路径
	 * @param outPathFile    压缩 文件/文件夹 输出路径+文件名 D:/xx.zip
	 * @param isDelSrcFile   是否删除原文件: 压缩前文件
	 */
	public static void toZip(String srcDir, String outPathFile,boolean isDelSrcFile) throws Exception {
		long start = System.currentTimeMillis();
		FileOutputStream out = null; 
		ZipOutputStream zos = null;
		try {
			out = new FileOutputStream(new File(outPathFile));
			zos = new ZipOutputStream(out);
			File sourceFile = new File(srcDir);
			if(!sourceFile.exists()){
				throw new Exception("需压缩文件或者文件夹不存在");
			}
			compress(sourceFile, zos, sourceFile.getName());
			if(isDelSrcFile){
				delDir(srcDir);
			}
			log.info("原文件:{}. 压缩到:{}完成. 是否删除原文件:{}. 耗时:{}ms. ",srcDir,outPathFile,isDelSrcFile,System.currentTimeMillis()-start);
		} catch (Exception e) {
			log.error("zip error from ZipUtils: {}. ",e.getMessage());
			throw new Exception("zip error from ZipUtils");
		} finally {
			try {
				if (zos != null) {zos.close();}
				if (out != null) {out.close();}
			} catch (Exception e) {}
		}
	}

	/**
	 * 递归压缩方法
	 * @param sourceFile 源文件
	 * @param zos zip输出流
	 * @param name 压缩后的名称
	 */
	private static void compress(File sourceFile, ZipOutputStream zos, String name)
			throws Exception {
		byte[] buf = new byte[BUFFER_SIZE];
		if (sourceFile.isFile()) {
			zos.putNextEntry(new ZipEntry(name));
			int len;
			FileInputStream in = new FileInputStream(sourceFile);
			while ((len = in.read(buf)) != -1) {
				zos.write(buf, 0, len);
			}
			zos.closeEntry();
			in.close();
		} else {
			File[] listFiles = sourceFile.listFiles();
			if (listFiles == null || listFiles.length == 0) {
				if (KeepDirStructure) {
					zos.putNextEntry(new ZipEntry(name + "/"));
					zos.closeEntry();
				}
			} else {
				for (File file : listFiles) {
					if (KeepDirStructure) {
						compress(file, zos, name + "/" + file.getName());
					} else {
						compress(file, zos, file.getName());
					}
				}
			}
		}
	}
	
	/**
	 * 解压文件到指定目录
	 */
	@SuppressWarnings({ "rawtypes", "resource" })
	public static boolean unZipFiles(String zipPath, String descDir) {
		log.info("文件:{}. 解压路径:{}. 解压开始.",zipPath,descDir);
		long start = System.currentTimeMillis();
		try{
			File zipFile = new File(zipPath);
			System.err.println(zipFile.getName());
			if(!zipFile.exists()){
				throw new IOException("需解压文件不存在.");
			}
			File pathFile = new File(descDir);
			if (!pathFile.exists()) {
				pathFile.mkdirs();
			}
			ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
			for (Enumeration entries = zip.entries(); entries.hasMoreElements();) {
				ZipEntry entry = (ZipEntry) entries.nextElement();
				String zipEntryName = entry.getName();
				System.err.println(zipEntryName);
				InputStream in = zip.getInputStream(entry);
				String outPath = (descDir + File.separator + zipEntryName).replaceAll("\\*", "/");
				System.err.println(outPath);
				// 判断路径是否存在,不存在则创建文件路径
				File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
				if (!file.exists()) {
					file.mkdirs();
				}
				// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
				if (new File(outPath).isDirectory()) {
					continue;
				}
				// 输出文件路径信息
				OutputStream out = new FileOutputStream(outPath);
				byte[] buf1 = new byte[1024];
				int len;
				while ((len = in.read(buf1)) > 0) {
					out.write(buf1, 0, len);
				}
				in.close();
				out.close();
			}
			log.info("文件:{}. 解压路径:{}. 解压完成. 耗时:{}ms. ",zipPath,descDir,System.currentTimeMillis()-start);
			return true;
		}catch(Exception e){
			log.info("文件:{}. 解压路径:{}. 解压异常:{}. 耗时:{}ms. ",zipPath,descDir,e,System.currentTimeMillis()-start);
			return false;
		}
	}
	
	// 删除文件或文件夹以及文件夹下所有文件
	public static void delDir(String dirPath) throws IOException {
		log.info("删除文件开始:{}.",dirPath);
		long start = System.currentTimeMillis();
		try{
			File dirFile = new File(dirPath);
			if (!dirFile.exists()) {
				return;
			}
			if (dirFile.isFile()) {
				dirFile.delete();
				return;
			}
			File[] files = dirFile.listFiles();
			if(files==null){
				return;
			}
			for (int i = 0; i < files.length; i++) {
				delDir(files[i].toString());
			}
			dirFile.delete();
			log.info("删除文件:{}. 耗时:{}ms. ",dirPath,System.currentTimeMillis()-start);
		}catch(Exception e){
			log.info("删除文件:{}. 异常:{}. 耗时:{}ms. ",dirPath,e,System.currentTimeMillis()-start);
			throw new IOException("删除文件异常.");
		}
	}

    /**
     * 解压rar格式压缩包。高版本的RAR文件会解压失败
     * 对应的是java-unrar-0.3.jar，但是java-unrar-0.3.jar又会用到commons-logging-1.1.1.jar
     */
    private static boolean unrar(String sourceRar,String destDir) throws Exception{
        Archive a = null;
        FileOutputStream fos = null;
        try{
            a = new Archive(new File(sourceRar));
            FileHeader fh = a.nextFileHeader();
            while(fh!=null){
                if(!fh.isDirectory()){
                    //1 根据不同的操作系统拿到相应的 destDirName 和 destFileName
                    String compressFileName = fh.getFileNameString().trim();
                    String destFileName = "";
                    String destDirName = "";
                    //非windows系统
                    if(File.separator.equals("/")){
                        destFileName = destDir + compressFileName.replaceAll("\\\\", "/");
                        destDirName = destFileName.substring(0, destFileName.lastIndexOf("/"));
                        //windows系统
                    }else{
                        destFileName = destDir + compressFileName.replaceAll("/", "\\\\");
                        destDirName = destFileName.substring(0, destFileName.lastIndexOf("\\"));
                    }
                    //2创建文件夹
                    File dir = new File(destDirName);
                    if(!dir.exists()||!dir.isDirectory()){
                        dir.mkdirs();
                    }
                    //3解压缩文件
                    fos = new FileOutputStream(new File(destFileName));
                    a.extractFile(fh, fos);
                    fos.close();
                    fos = null;
                }
                fh = a.nextFileHeader();
            }
            a.close();
            a = null;
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }finally{
            if(fos!=null){
                try{fos.close();fos=null;}catch(Exception e){e.printStackTrace();return false;}
            }
            if(a!=null){
                try{a.close();a=null;}catch(Exception e){e.printStackTrace();return false;}
            }
        }
    }
    /**
     * 解压缩
     */
    public static boolean deCompress(String sourceFile,String destDir, String cmdPath) {
        boolean isUnZip = false;
        try {
            //保证文件夹路径最后是"/"或者"\"
            char lastChar = destDir.charAt(destDir.length()-1);
            if(lastChar!='/'&&lastChar!='\\'){
                destDir += File.separator;
            }
            //根据类型，进行相应的解压缩
            String type = sourceFile.substring(sourceFile.lastIndexOf(".")+1);
            if(type.equals("zip")){
                isUnZip = ZipUtil.unZipFiles(sourceFile, destDir);
            }else if(type.equals("rar")){
                //isUnZip = ZipUtil.unrar(sourceFile, destDir);
                isUnZip = realExtract(new File(sourceFile), destDir, cmdPath);
            }else{
                log.error("只支持zip和rar格式的压缩包！");
                isUnZip = false;
            }
            return isUnZip;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 采用命令行方式解压文件
     * @param zipFile 压缩文件
     * @param destDir 解压结果路径
     * @param cmdPath WinRAR.exe的路径，也可以在代码中写死
     * @return
     */
    public static boolean realExtract(File zipFile, String destDir,String cmdPath) {
        // 解决路径中存在/..格式的路径问题
        destDir = new File(destDir).getAbsoluteFile().getAbsolutePath();
        while(destDir.contains("..")) {
            String[] sepList = destDir.split("\\\\");
            destDir = "";
            for (int i = 0; i < sepList.length; i++) {
                if(!"..".equals(sepList[i]) && i < sepList.length -1 && "..".equals(sepList[i+1])) {
                    i++;
                } else {
                    destDir += sepList[i] + File.separator;
                }
            }
        }

        boolean bool = false;
        if (!zipFile.exists()) {
            return false;
        }

        // 开始调用命令行解压，参数-o+是表示覆盖的意思
        //cmdPath = "E:\\workspace\\operation-uprr-manage\\src\\main\\resources\\cmd\\WinRAR.exe";
        String cmd = cmdPath + " X -o+ " + zipFile + " " + destDir;
        System.out.println(cmd);
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            if (proc.waitFor() != 0) {
                if (proc.exitValue() == 0) {
                    bool = false;
                }
            } else {
                bool = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("解压" + (bool ? "成功" : "失败"));
        return bool;
    }

}
